package com.papper.common.service.sys;

import com.google.common.collect.Lists;
import com.papper.common.bean.sys.SysCode;
import com.papper.common.cache.CacheConstants;
import com.papper.common.mapper.sys.SysCodeMapper;
import com.papper.common.service.ServiceImpl;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 系统字典业务
 */
@Service("sysCodeService")
public class SysCodeService extends ServiceImpl<SysCodeMapper, SysCode> {

	@Override
	protected void clearCaches(SysCode entity) {
		super.clearCaches(entity);
		String model = getModel();
		cache.delete(CacheConstants.Keys(model, entity.getCodeKey()));
		cache.delete(CacheConstants.Keys(model, entity.getCodeKey(), entity.getParentId()));
		cache.delete(CacheConstants.Keys(model, "PA", entity.getId()));
		cache.delete(CacheConstants.Keys(model, "PA", entity.getParentId()));
	}

	/**
	 * 根据名称获取字典信息.
	 *
	 * @param name the name
	 * @return the sys code
	 */
	public SysCode findByKey(final String name) {
		SysCode rtn     = null;
		Example example = new Example(SysCode.class);
		example.createCriteria().andEqualTo("codeKey", name);
		example.setOrderByClause("id desc limit 1");
		rtn = findOne(example);
		return rtn;
	}

	/**
	 * 根据主键获取字典信息.
	 *
	 * @param key      the key
	 * @param parentId the parent id
	 * @return the sys code
	 */
	public SysCode findByKey(final String key, final Long parentId) {
		SysCode rtn     = null;
		Example example = new Example(SysCode.class);
		example.createCriteria().andEqualTo("codeKey", key).andEqualTo("parentId", parentId);
		example.setOrderByClause("id desc limit 1");
		rtn = findOne(example);
		return rtn;
	}

	/**
	 * 获取子类字典列表.
	 *
	 * @param parentId the parent id
	 * @return the list
	 */
	public List<SysCode> findByParentId(final Long parentId) {
		List<SysCode> rtn     = null;
		Example       example = new Example(SysCode.class);
		example.createCriteria().andEqualTo("parentId", parentId);
		example.setOrderByClause("code_sort asc");
		rtn = find(example);
		return rtn;
	}

	/**
	 * 获取子类字典列表.
	 *
	 * @param parentKey the parent key
	 * @return the list
	 */
	public List<SysCode> findChild(String parentKey) {
		SysCode sysCode = findByKey(parentKey);
		if (sysCode == null) {
			return null;
		}
		return findByParentId(sysCode.getId());
	}

	/**
	 * 根据关键值获取字典信息.
	 *
	 * @param parentKey the parent key
	 * @param key       the key
	 * @return the sys code
	 */
	public SysCode findByKey(String parentKey, String key) {
		SysCode sysCode = findByKey(parentKey);
		if (sysCode == null) {
			return null;
		}
		sysCode = findByKey(key, sysCode.getId());
		return sysCode;
	}

	/**
	 * 更新系统字典信息.
	 *
	 * @param sysCode the sys code
	 * @param key     the key
	 * @param value   the value
	 * @param desc    the desc
	 */
	public void modifyKeyValue(SysCode sysCode, String key, String value, String desc) {
		if (sysCode == null) {
			SysCode add = new SysCode();
			add.setCodeKey(key);
			add.setCodeValue(value);
			add.setCodeDesc(desc);
			add.setCreateTime(new Date());
			add.setParentId(0);
			add.setDataStatus(0);
			add.setModifyTime(new Date());
			save(add);
		} else {
			SysCode upd = new SysCode();
			upd.setId(sysCode.getId());
			upd.setCodeKey(key);
			upd.setCodeValue(value);
			upd.setModifyTime(new Date());
			updateNoLog(upd);
		}
	}

	/**
	 * Find rate type list.
	 *
	 * @param accountType the account type
	 * @return the list
	 */
	public List<SysCode> findRateType(String accountType) {
		List<SysCode> sysCodes = Lists.newArrayList();
		if ("1".equals(accountType)) {
			return findChild("OrgType");
		} else if ("2".equals(accountType)) {
			// 终端商户
			SysCode temp = new SysCode();
			temp.setCodeKey("L90");
			temp.setCodeValue("终端商户");
			sysCodes.add(temp);
		} else if ("3".equals(accountType)) {
			// 终端商户
			SysCode temp = new SysCode();
			temp.setCodeKey("L91");
			temp.setCodeValue("通道外放");
			sysCodes.add(temp);
		}
		return sysCodes;
	}
}